<?php
/**
 * @package WordPress
 * @subpackage desire
 */
?>
<?php
// Autotmatic feed links support
//==============================
add_theme_support( 'automatic-feed-links' );

//add_editor_style();

// Custom post format support
//===========================
add_theme_support( 'post-formats', array( 'gallery' ) );

// Post Thumbnail support
//=======================
add_theme_support( 'post-thumbnails' );

// Remove excerpt brackets
//=========================
function desire_strip_excerpt($more) {
    return '...';
}
add_filter('excerpt_more', 'desire_strip_excerpt');

// Support for language translation
//=================================
load_theme_textdomain( 'desire', TEMPLATEPATH . '/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
    require_once( $locale_file );

// Support for editor style
//========================
add_editor_style();

// Content width
//==============
if(!isset($content_width))
    // To increase or decrease the width of main content, modify this value
	// QAPRC resets, originally being 640 by default
	$content_width = 850; 
	
// Initializing the options variable
//==================================
global $desireoptions;
$desireoptions = get_option('desire_options');
if(empty($desireoptions))
    $desireoptions = desire_init_options();

// Logo Options
//==============
global $desirelogo;
$desirelogo = get_option('desire_logo');
define('DESIRE_MAX_UPLOAD_SIZE', 150);
if(trim($desirelogo) == "")
    desire_init_logo();
    
function desire_init_logo() {
    global $desirelogo;
    $desirelogo = get_template_directory_uri().'/images/desire_logo.png';
}

// Initialize Theme Options
//=========================
function desire_init_options() {
    global $desireoptions;
    return array(
        'body_font' => 'Helvetica, Arial, sans-serif',
        'enable_custom_background' => false,
        'enable_post_slideshow' => true,
        'slideshow_count' => 5,
        'enable_custom_header' => false,
        'site_title_font' => 'default',
        'enable_breadcrumbs' => true,
        'sidebar_width' => 250,
        'adsense_code' => '',
        'show_tabbed_content' => true,
        'blog_layout' => 'traditional',
        'show_category_front' => false,
        'show_category_single' => true,
        'show_tags_front' => false,
        'show_tags_single' => true
    );
}

// Calling the admin page
//=======================
require_once ( get_stylesheet_directory() . '/adminform.php' );

// Custom header feature
//=======================
define('HEADER_TEXTCOLOR', '333333');
define('HEADER_IMAGE', '%s/images/headers/bubbles1.jpg'); // %s is the template dir uri
define('HEADER_IMAGE_WIDTH', desire_get_wrapper_width());
define('HEADER_IMAGE_HEIGHT', 150);

function desire_header_style() {
    $logotxtstyle = "";
    if ('blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) or '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR))
        $logotxtstyle = 'display: none;';
    else
        $logotxtstyle = 'color: #'.get_theme_mod('header_textcolor', HEADER_TEXTCOLOR).'; text-shadow: none;';
    echo '<style type="text/css">'."\n";
    echo '#header, #headimg { background: #fff url('.get_header_image().') repeat; overflow: hidden; width: '.HEADER_IMAGE_WIDTH.'px; height: '.HEADER_IMAGE_HEIGHT.'px; }'."\n";
    echo '#header h1 { '.$logotxtstyle.' font-size: 2em; font-weight: 300; margin-top: 50px; margin-left: 20px; font-family: Times New Roman, serif; }'."\n";
    echo '#headimg h1 { '.$logotxtstyle.' font-size: 2.5em; font-weight: 300; margin-top: 50px; margin-left: 20px; font-family: Times New Roman, serif; }'."\n";
    echo '#header h1 a, #headimg h1 a { '.$logotxtstyle.' text-decoration: none; }'."\n";
    echo '.description { '.$logotxtstyle.' font-size: 0.813em; margin-left: 20px; font-family: Helvetica, Arial, sans-serif; margin-top: 10px; }'."\n";
    echo '#desc { '.$logotxtstyle.' font-size: 1em; margin-left: 20px; font-family: Helvetica, Arial, sans-serif; margin-top: 10px; }'."\n";
    echo '</style>'."\n";
}

if($desireoptions['enable_custom_header'])
    add_custom_image_header('desire_header_style', 'desire_header_style');

// Registering default header images
register_default_headers(array(
    'bubbles1' => array(
            'url' => '%s/images/headers/bubbles1.jpg',
            'thumbnail_url' => '%s/images/headers/bubbles1_thumbnail.jpg',
            'description' => __( 'Bubbles 1', 'desire' )
    ),
    'bubbles2' => array(
        'url' => '%s/images/headers/bubbles2.jpg',
        'thumbnail_url' => '%s/images/headers/bubbles2_thumbnail.jpg',
        'description' => __('Bubbles 2','desire')
    )
));

// Custom background feature
//==========================
function desire_custom_background() {
    $bgimage = get_background_image();
    $bgcolor = get_background_color();
    $bgrepeat = get_theme_mod('background_repeat', 'repeat');
    $bgattachment = get_theme_mod('background_attachment', 'scroll');
    echo '<style type="text/css">'."\n";
    if(!empty($bgimage)) { ?>        
        body { background-image: url(<?php echo $bgimage; ?>); }        
    <?php } if(!empty($bgcolor)) { ?>        
        body { background-color: #<?php echo $bgcolor; ?>; }        
    <?php } if(!empty($bgrepeat) and !empty($bgimage)) { ?>        
        body { background-repeat: #<?php echo $bgrepeat; ?>; }        
    <?php } if(!empty($bgattachment) and !empty($bgimage)) { ?>        
        body { background-attachment: <?php echo $bgattachment; ?>; }        
    <?php
    }
    echo '</style>'."\n";
}
if(function_exists('add_custom_background') and $desireoptions['enable_custom_background']) {
    add_custom_background('desire_custom_background');
}

// Registering Sidebars
//=====================
function desire_register_sidebar_widgets() {
    register_sidebar(array(
        'name' => __('Primary Widget Area 1','desire'),
        'id' => 'desire-primary-widget-1',
        'description' => __('Primary Widget Area top of tabbed content','desire'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => __('Primary Widget Area 2','desire'),
        'id' => 'desire-primary-widget-2',
        'description' => __('Primary Widget Area below tabbed content','desire'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => __('Footer Widget Area 1','desire'),
        'id' => 'desire-footer-widget-1',
        'description' => __('Footer Widget Area 1','desire'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => __('Footer Widget Area 2','desire'),
        'id' => 'desire-footer-widget-2',
        'description' => __('Footer Widget Area 2','desire'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => __('Footer Widget Area 3','desire'),
        'id' => 'desire-footer-widget-3',
        'description' => __('Footer Widget Area 3','desire'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
        'name' => __('Footer Widget Area 4','desire'),
        'id' => 'desire-footer-widget-4',
        'description' => __('Footer Widget Area 4','desire'),
        'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
        'after_widget' => '</li>',
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
    ));
}
add_action('widgets_init', 'desire_register_sidebar_widgets');

// Removing gallery inline styles
//===============================
add_filter( 'use_default_gallery_style', '__return_false' );
function desire_remove_gallery_css( $css ) {
	return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css );
}
// Backwards compatibility with WordPress 3.0.
if ( version_compare( $GLOBALS['wp_version'], '3.1', '<' ) )
	add_filter( 'gallery_style', 'desire_remove_gallery_css' );

// Calculate wrapper width
//========================
function desire_get_wrapper_width() {
    global $desireoptions;
    if(is_page_template('onecolumn-page.php') or is_404()) {
        $wrapper_width = desire_get_container_width();
    } else {
        $wrapper_width = desire_get_container_width() + $desireoptions['sidebar_width'] + 10; // the number being the margin of sidebar
    }    
    return $wrapper_width;
}

function desire_get_container_width() {
    global $content_width;
    if(is_page_template('onecolumn-page.php') or is_404()) {
        $container_width = 750;  //
    } else {
        $container_width = $content_width;
    }    
    return $container_width;
}

// Add custom style and header files to wp head
//=============================================
function desire_add_header_files() {
    global $desireoptions;
    if($desireoptions['site_title_font'] != 'default')
        echo '<link href="http://fonts.googleapis.com/css?family='.$desireoptions['site_title_font'].'" rel="stylesheet" type="text/css">'."\n";
    echo '<style type="text/css">'."\n";
    echo 'body, input, textarea { font-family: '.$desireoptions['body_font'].'; }'."\n";
    if($desireoptions['body_font'] == 'Times New Roman, serif')
        echo 'body { font-size: 1.25em; }'."\n";
    echo '#wrapper { width: '.desire_get_wrapper_width().'px; }'."\n";
    echo '#container { width: '.desire_get_container_width().'px; }'."\n";
    echo '#sidebar { width: '.$desireoptions['sidebar_width'].'px; margin-left: 20px; }'."\n";
    echo '#desire_slide, #desire_slide a { width: '.desire_get_container_width().'px; }'."\n";
    echo '.entry-content img { max-width: '.(desire_get_container_width() - 10).'px; }'."\n"; //Subtracting 10 for the image border
    echo '.sticky .entry-content img { max-width: '.(desire_get_container_width() - 40).'px; }'."\n"; //Subtracting 10 for the image border
    echo '.entry-content .wp-caption { max-width: '.(desire_get_container_width() - 22).'px; }'."\n";
    echo '.entry-content .wp-caption img { max-width: '.(desire_get_container_width() - 32).'px; }'."\n";
    echo '.sticky .entry-content .wp-caption { max-width: '.(desire_get_container_width() - 52).'px; }'."\n";
    echo '.sticky .entry-content .wp-caption img { max-width: '.(desire_get_container_width() - 62).'px; }'."\n";
    if($desireoptions['site_title_font'] != 'default')
        echo '#header h1 { font-family: "'.$desireoptions['site_title_font'].'", Arial, sans-serif; }'."\n";
    else
        echo '#header h1 { font-family: Times New Roman, serif; }'."\n";
    echo '.twomag { width: '.(round(desire_get_container_width()/2) - 10).'px; }'."\n"; // Dividing by two for two columns and subtracting two 10px for the margin between them
    echo '#twomag-leftcol { margin-right: 10px; }'."\n";
    echo '#twomag-rightcol { margin-left: 10px; }'."\n";
    if(is_page_template('onecolumn-page.php') or is_404())
        echo '#header, #headimg { width: '.desire_get_wrapper_width().'px; }'."\n";
    echo '</style>'."\n";
    echo '<script type="text/javascript" src="'.get_template_directory_uri().'/js/desire.js"></script>'."\n";
    if($desireoptions['enable_post_slideshow'] and is_front_page())
        echo '<script type="text/javascript" src="'.get_template_directory_uri().'/js/postslideshow.js"></script>'."\n";    
}
add_action('wp_head','desire_add_header_files');

// Custom Navigation Menu
//=======================
if ( function_exists( 'register_nav_menu' ) ) {
    register_nav_menus(array('primary' => __( 'Primary Navigation', 'desire' )));
}

// QAPRC: the Language Switcher 
function qaprc_lang_switch($items) {
	global $lang_switch;										
					$lang_switch = '<form name="lang_form" style="float:right;">';
					$lang_switch .= '<select name="plugin_language" onchange="this.form.submit()" method="get" >';
					$lang_switch .=	'<option value="" selected="selected">Choose Language</option>';
					$lang_switch .=	'<option value="zh_HK">Traditional Chinese</option>';
					$lang_switch .=	'<option value="zh_CN">Simplified Chinese</option>';
					$lang_switch .=	'<option value="en_US">English</option>';
					$lang_switch .=	'</select>';	
					$lang_switch .='</form>';
	echo $lang_switch;				
}
	
// Breadcrumbs feature
//====================
function desire_breadcrumbs() {
    global $post;
    echo '<a href="';
    echo get_home_url();
    echo '">';
    _e('Home','desire');
    echo '</a>';
    echo '<span class="breadcrumb_arrow">&raquo;</span>';
    if (is_category() || is_single()) {
        single_cat_title();
        if (is_single()) {
            _e(desire_single_category($post->ID));
			echo '<span class="breadcrumb_arrow">&raquo;</span>';
            the_title();
        }
    } elseif (is_tag()) {
        echo single_tag_title();
    } elseif (is_page()) {
        echo the_title();
    } elseif (is_day()) {
        echo __('Archive for ','desire'); the_time('F jS, Y');
    } elseif (is_month()) {
        echo __('Archive for ','desire'); the_time('F, Y');
    } elseif (is_year()) {
        echo __('Archive for ','desire'); the_time('Y');
    } elseif (is_author()) {
        echo __('Author Archive','desire');
    } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {
        echo __('Blog Archives','desire');
    } elseif (is_search()) {
        echo __('Search Results for ','desire').get_search_query();
    } elseif(is_single()) {
        the_title();
    }
}

function desire_single_category($postid) {
    $category = get_the_category($postid);
    $parentpost = get_post($postid);
    $parentid = $parentpost->post_parent;
    if(empty($category))
        return '<a href="'.get_permalink($parentid).'">'.get_the_title($parentid).'</a>';
    else
        return '<a href="'.get_category_link($category[0]->cat_ID).'">'.$category[0]->cat_name.'</a>';
}

// Getting the file extension
//==========================
function desire_get_file_ext($str) {
    $i = strrpos($str,".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}

// Uploading the logo to uploads dir
//==================================
function desire_upload_logo($fileelem) {
    global $desirelogo;
    $extension = desire_get_file_ext($fileelem['name']);
    $success = true;
    $message = "";
    if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
        $message = '<div class="formerror">'.__('Not a valid image file','desire').'</div>';
        return $message;
    } else {
        $size = filesize($fileelem['tmp_name']);
        if($size > DESIRE_MAX_UPLOAD_SIZE*1024) {
            $message = '<div class="formerror">'.__('You have exceeded '.DESIRE_MAX_UPLOAD_SIZE.' KB size limit of the logo','desire').'</div>';
            return $message;
        }
        $overrides = array( 'test_form' => false);
        $file = wp_handle_upload($_FILES['desire_logo'], $overrides);
        $desirelogo = $file['url'];
        update_option('desire_logo',$desirelogo);
        $message = '<div class="success">'.__('Your logo has been uploaded','desire').'</div>';
        return $message;
    }
}

// Popular posts
//==============
function desire_popular_posts() {
    $popularposts = get_posts('numberposts=5&orderby=comment_count&order=DESC&post_type=post');
    $output = "";
    foreach($popularposts as $popular) {
        setup_postdata($popular);
        $id = $popular->ID;
        $title = $popular->post_title;
        $link = get_permalink($popular->ID);
        if(!is_search() and !is_404())
            $excerpt = desire_trim_words(get_the_excerpt(),50);
        else $excerpt = "";
        $output .= '<li class="popular-post"><a class="popular-title" href="'.$link.'">'.$title.'</a><p class="popular-excerpt">'.$excerpt.'</p></li>'."\n";
    }
    echo $output;
}

// Recent comments
//================
function desire_recent_comments() {
    $args = array('number' => 5, 'order' => 'DESC', 'type' => 'comment');
    $recentcomments = get_comments($args);
    $output = "";
    foreach($recentcomments as $entry) {
        $author = $entry->comment_author;
        $commenttxt = desire_trim_words($entry->comment_content,50);
        $commentpost = get_post($entry->comment_post_ID);
        $posttitle = $commentpost->post_title;
        $output .= '<li class="recent-comment"><span class="recent-comment-author">'.$author.' says:</span> '.$commenttxt.' on &quot;'.$posttitle.'&quot;</li>'."\n";
    }
    echo $output;
}

// Trim words from content
//========================
function desire_trim_words($str, $n, $delim='...') {
   $len = strlen($str);
   if ($len > $n) {
       preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
       return rtrim($matches[1]) . $delim;
   }
   else {
       return $str;
   }
}

// Post Slideshow
//===============
function desire_post_slider() {
    global $desireoptions;
    $args = array('showposts' => $desireoptions['slideshow_count']);
    $sliderposts = get_posts($args);
    $sliderwidth = desire_get_container_width();
    $output = "";
    $first = true;
    if($sliderposts) {
        $output = '<div id="desire_slide">'."\n";
        foreach($sliderposts as $entry) {
            setup_postdata($entry);
            $title = $entry->post_title;
	    $excerpt = get_the_excerpt();
	    $link = get_permalink($entry->ID);
	    $date = $entry->post_date;
            $attachmentid = get_post_thumbnail_id($entry->ID);
            $attachment = wp_get_attachment_image_src($attachmentid,'full',false);
            $imageurl = $attachment[0];
            if($imageurl == "")
                $imageurl = get_template_directory_uri().'/images/default_feature.png';
            if($first)
                $class = 'class="start"';
            else $class = "";
            $output .= '<a href="'.$link.'" '.$class.'>'."\n";
            $output .= '<img width="'.$sliderwidth.'" src="'.$imageurl.'" rel="<h3>'.$title.'</h3>'.desire_trim_words($excerpt,200).'"/>'."\n";
            $output .= '</a>';
            $first = false;
        }
        $output .= '<div class="caption"><div class="content"></div></div>'."\n";
        $output .= '</div><div class="clear"></div>';
    }
    return $output;
}

function desire_display_slideshow() {
    global $desireoptions;
    if(is_front_page() and $desireoptions['enable_post_slideshow'])
        echo desire_post_slider();
}

// Paged Navigation
//=================
function desire_get_pagination($range = 4){
  global $paged, $wp_query;
  $max_page = "";
  if ( !$max_page ) {
    $max_page = $wp_query->max_num_pages;
  }
  if($max_page > 1){
    echo '<div class="navigation">'."\n";
    if(!$paged){
      $paged = 1;
    }
    if($paged != 1){      
      echo "<a href=" . get_pagenum_link(1) . ">".__(' First ','desire')."</a>";
    }
    previous_posts_link(' <span class="navigation-sep">&laquo;</span> ');
    if($max_page > $range){
      if($paged < $range){
        for($i = 1; $i <= ($range + 1); $i++){
          echo "<a href='" . get_pagenum_link($i) ."'";
          if($i==$paged) echo " class='current'";
          echo ">$i</a>";
        }
      }
      elseif($paged >= ($max_page - ceil(($range/2)))){
        for($i = $max_page - $range; $i <= $max_page; $i++){
          echo "<a href='" . get_pagenum_link($i) ."'";
          if($i==$paged) echo " class='current'";
          echo ">$i</a>";
        }
      }
      elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
        for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
          echo "<a href='" . get_pagenum_link($i) ."'";
          if($i==$paged) echo " class='current'";
          echo ">$i</a>";
        }
      }
    }
    else{
      for($i = 1; $i <= $max_page; $i++){
        echo "<a href='" . get_pagenum_link($i) ."'";
        if($i==$paged) echo " class='current'";
        echo ">$i</a>";
      }
    }
    next_posts_link(' <span class="navigation-sep">&raquo;</span> ');
    if($paged != $max_page){
      echo " <a href=" . get_pagenum_link($max_page) . ">".__(' Last ','desire')."</a>";
    }
    echo '</div>'."\n";
  }
}

// Social Bookmarking
//===================
function desire_bookmarks() {
    define("DESIRE_SHARE_TITLE",get_the_title());
    define("DESIRE_SHARE_PERMALINK",get_permalink());
    define("DESIRE_SHARE_EXCERPT",htmlspecialchars(get_the_excerpt()));
    $social_bookmarks = array(
        array('name' => __( 'facebook','desire' ), 'icon' => 'facebook.png', 'bookmarkurl' => 'http://www.facebook.com/share.php?u='.DESIRE_SHARE_PERMALINK.'&amp;t='.DESIRE_SHARE_TITLE),
        array('name' => __( 'twitter','desire' ), 'icon' => 'twitter.png', 'bookmarkurl' => 'http://twitter.com/home?status='.DESIRE_SHARE_TITLE.'%20-%20'.DESIRE_SHARE_PERMALINK),
        array('name' => __( 'delicious','desire' ), 'icon' => 'delicious.png', 'bookmarkurl' => 'http://delicious.com/post?url='.DESIRE_SHARE_PERMALINK.'&amp;title='.DESIRE_SHARE_TITLE.'&amp;notes='.DESIRE_SHARE_EXCERPT),
        array('name' => __( 'digg','desire' ), 'icon' => 'digg.png', 'bookmarkurl' => 'http://digg.com/submit?phase=2&amp;url='.DESIRE_SHARE_PERMALINK.'&amp;title='.DESIRE_SHARE_TITLE.'&amp;bodytext='.DESIRE_SHARE_EXCERPT),
        array('name' => __( 'google','desire' ), 'icon' => 'google.png', 'bookmarkurl' => 'http://www.google.com/bookmarks/mark?op=edit&amp;bkmk='.DESIRE_SHARE_PERMALINK.'&amp;title='.DESIRE_SHARE_TITLE.'&amp;annotation='.DESIRE_SHARE_EXCERPT),
        array('name' => __( 'reddit','desire' ), 'icon' => 'reddit.png', 'bookmarkurl' => 'http://reddit.com/submit?url='.DESIRE_SHARE_PERMALINK.'&amp;title='.DESIRE_SHARE_TITLE),
        array('name' => __( 'stumbleupon','desire' ), 'icon' => 'stumbleupon.png', 'bookmarkurl' => 'http://www.stumbleupon.com/submit?url='.DESIRE_SHARE_PERMALINK.'&amp;title='.DESIRE_SHARE_TITLE),
        array('name' => __( 'email','desire' ), 'icon' => 'email.png', 'bookmarkurl' => 'mailto:?subject='.DESIRE_SHARE_TITLE.'&body='.DESIRE_SHARE_PERMALINK)
    );
    $bookmarklist = '<div class="post-bookmark"><span class="share-title">'.__('Share this on:','desire').'</span>';
    foreach($social_bookmarks as $bookmark) {
       $bookmarklist .= '<a class="share-bookmark" href="'.$bookmark['bookmarkurl'].'">'.ucwords($bookmark['name']).'</a>'."\n"; 
    }
    $bookmarklist .= '</div>'."\n";
    echo $bookmarklist;
}

// Odd post or Even post for two column layout
//============================================
global $postcount;
function desire_post_odd_even() {
    global $postcount;
    $postcount++;
    if($postcount % 2 == 0)
        $postclass = 'post-even';
    else
        $postclass = 'post-odd';    
    return $postclass;
}

// Returns count of post on a page
//================================
function desire_count_post() {
    global $postcount;
    return $postcount;
}

// Comment callback function
//===========================
function desire_comment_callback($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
    $commenttype = $comment->comment_type;
    ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
        <div id="comment-<?php comment_ID(); ?>">
            <?php
                if($commenttype == "") {
                    preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', get_avatar($comment,48), $result);
                    $gravatar_url = $result[1];
                }
            ?>
            <?php if($commenttype == ""): ?>
                <div class="comment-gravatar" style="background:url(<?php echo $gravatar_url; ?>) no-repeat top left;"><img width="48px" height="48px" src="<?php echo get_template_directory_uri(); ?>/images/gravatar_bg.png" /></div>
            <?php endif; ?>
            <div class="comment-meta">
                <p class="comment-author"><?php echo get_comment_author_link(); ?></p>
                <p class="comment-date"><?php _e('Replied on: ','desire'); ?><?php echo get_comment_date().", "; echo get_comment_time(); ?><?php edit_comment_link( __( '(Edit)', 'desire' ), ' ' ); ?></p>
                <?php if ( $comment->comment_approved == '0' ) : ?><p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'desire' ); ?></p><?php endif; ?>
            </div>
            <div class="comment-reply"><?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?></div>
            <div class="clear"></div>            
            <div class="comment-body"><?php comment_text(); ?></div>
        </div>
    <?php
}  
?>